package local.blueheart.springcloud.bhmicroserviceorder.service;

import local.blueheart.springcloud.bhmicroserviceorder.pojo.Item;
import local.blueheart.springcloud.bhmicroserviceorder.pojo.Order;
import local.blueheart.springcloud.bhmicroserviceorder.pojo.OrderDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class OrderService {

    private static Map<String, Order> MAP = new HashMap<String,Order>();

    @Autowired
    private ItemService itemService;

    static  {
        Order order = new Order();
        order.setOrderId("111222333");
        order.setCreateDate(new Date());
        order.setUpdateDate(order.getCreateDate());
        order.setUserId(1L);
        List<OrderDetail> orderDetails = new ArrayList<>();

        Item item = new Item();
        item.setId(1L);
        orderDetails.add(new OrderDetail(order.getOrderId(),item));


        item = new Item();
        item.setId(2L);
        orderDetails.add(new OrderDetail(order.getOrderId(),item));
        order.setOrderDetails(orderDetails);
        MAP.put("111222333",order);
    }

    public Order queryOrderById(String orderId){
        Order order = MAP.get(orderId);
        if (null == order){
            return null;
        }
        List<OrderDetail> orderDetails = order.getOrderDetails();
        for(OrderDetail orderDetail:orderDetails){
            Item item = this.itemService.queryItemById(orderDetail.getItem().getId());
            if (null == item){
                continue;
            }
            orderDetail.setItem(item);
        }
        return order;
    }

}
